package com.example.mall.service.impl;


import com.example.mall.mapper.CategoryMapper;
import com.example.mall.pojo.*;
import com.example.mall.service.CategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class CategoryServiceImpl implements CategoryService {

    CategoryMapper categoryMapper;

    @Override
    public List<Category> getCategory(Integer type) {
        List<Category> categories = categoryMapper.selectByType(type);
        if(categories==null){
            return null;
        }
        return categories;

    }

    @Override
    public List<Level1> getCategoryLevelFirst() {
        List<Level1> level1 = categoryMapper.selectByTypeFirst1();
        return level1;
    }

    @Override
    public List<Level1> getCategoryLevelFirst1(Integer pagenum, Integer pagesize) {
        PageHelper.startPage(pagenum,pagesize);
        List<Level1> level1 = categoryMapper.selectByTypeFirst1();
        return level1;
    }


    @Override
    public List<Level1> getCategoryLevelFirst2(Integer pagenum, Integer pagesize) {
//        PageHelper.startPage(pagenum,pagesize);
        List<Level1> level1 = categoryMapper.selectByTypeFirst1();
        return level1;
    }

    @Override
    public PageInfo<Level1> getPageAll2(Integer pagenum, Integer pagesize) {
        List<Category> categories = categoryMapper.selectBy();
        List<Level1> level1 = categoryMapper.selectByTypeFirst1();
        PageInfo<Level1> level1PageInfo = new PageInfo<>(level1);
        return level1PageInfo;
    }

    @Override
    public List<Level2> getCategoryLevelSecond(Integer id) {
        List<Level2> level2 = categoryMapper.selectByTypeSecond(id);
        return level2;
    }

    @Override
    public List<Level3> getCategoryLevelThird(Integer id) {
        List<Level3> level3 = categoryMapper.selectByTypeThird(id);
        return level3;
    }


    @Override
    public List<Category> getCategoryAll(Integer pagenum, Integer pagesize) {
        PageHelper.startPage(pagenum,pagesize);
        List<Category> categories = categoryMapper.selectBy();
        if(categories==null){
            return null;
        }
        return categories;
    }



    @Override
    public PageInfo<Level1> getPageAll(Integer pagenum, Integer pagesize) {
        List<Category> categories = categoryMapper.selectBy();
        List<Level1> level1 = categoryMapper.selectByTypeFirst1();
        PageInfo<Level1> level1PageInfo = new PageInfo<>(level1);
        return level1PageInfo;
    }



    @Override
    public Boolean addCategory(Integer pId, String name, Integer level) {
        int i = categoryMapper.insertCategory(pId, name, level);
        if(i==1){
            return true;
        }
        return false;
    }

    @Override
    public Category getCategoryById(Integer catId) {
        Category category = categoryMapper.selectById(catId);
        if(category==null){
            return  null;
        }

        return category;
    }

    @Override
    public Boolean updateCategory(Integer catId, String catName) {
        int category = categoryMapper.updateById(catName,catId);
        if(category==1){
            return true;
        }
        return false;
    }

    @Override
    public Boolean deleteCategory(Integer catId) {
        int i = categoryMapper.deleteById(catId);
        if(i==1){
            return true;
        }
        return false;
    }

    @Override
    public Category getCategoryName(String name) {
        Category category = categoryMapper.selectByName(name);
        if(category==null){
            return null;
        }
        return category;
    }
}
